<?php


class EntryTable extends Doctrine_Table {
    public static function getInstance(){
        return Doctrine_Core::getTable('Entry');
    }

	# experimental - query class to chain
	public function __construct($name, Doctrine_Connection $conn, $initDefinition = false){
		$this->setAttribute(Doctrine_Core::ATTR_QUERY_CLASS, str_replace("Table", "Query", get_class($this)));
		return parent::__construct($name, $conn, $initDefinition);
	}


	public function createQueryWithCommons($alias = ''){
    	$q=$this->createQuery($alias);
//    	$q=$this->getSortedQuery("ASC", $q); // visiem querijiem pa virsu jāliek sortable no Sortable plugina
        $q->orderBy("position ASC");
    	return $q;
    }

    public function findOneBySlug($slug){
        $q=$this->createQuery();
        $q->addWhereSlug($slug);
        return $q->execute();
    }

    // for DoctrineRouting
    public function getListQueryRouted($routingParams){
		$context=sfContext::getInstance();

		if (!empty($routingParams["section_slug"])){
			$section=Doctrine_Core::getTable("Section")->findOneBySlug($routingParams["section_slug"]);
			if ($section=="") return;
			$context->set("section", $section);
		}

		if (!empty($routingParams["tag_slug"])){
			$tag=Doctrine_Core::getTable("Tag")->findOneBySlug($routingParams["tag_slug"]);
			if ($tag=="") return;
			$context->set("tag", $tag);
		}

		$q=$this->createQueryWithCommons();
		if (!empty($section)){
			$q->addWhereSection($section);
		}

		if (!empty($tag)){
			$q=PluginTagTable::getObjectTaggedWithQuery("Entry", array($tag->getName()), $q);
		}

		return $q;
    }

    public function findOneBySlugRouted($routingParams){
        if (!empty($routingParams["slug"])){
            return $this->findOneBySlug($routingParams["slug"]);
        }
    }




}